/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtGui>

#include "vdrthread.h"

VDRThread::VDRThread( QObject *parent ) : QThread( parent ),
    m_vdrFindServer( 0L ),
    m_vdr( 0L ),
    m_eventLoopReady( false )
{
    connect( this, SIGNAL( vdrNeeded() ),
             this, SLOT(   initVDR() ) );
    connect( this, SIGNAL( vdrFindServerNeeded() ),
             this, SLOT(   initVDRFindServer() ) );
}


void VDRThread::waitForEventLoop()
{
    qDebug() << "VDRThread::waitForEventLoop started";
    m_mutex.lock();

    if ( !m_eventLoopReady )
    {
        m_waitingForEventLoop.wait( &m_mutex );
    }

    m_mutex.unlock();

    qDebug() << "VDRThread::waitForEventLoop ended";
}


void VDRThread::run()
{
    qDebug() << "VDRThread::run started";

    QTimer::singleShot( 0, this, SLOT( eventLoopReady() ) );
    exec();

    qDebug() << "VDRThread::run eventLoop ended";

    m_eventLoopReady = false;

    qDebug() << "VDRThread::run ended";
}


void VDRThread::eventLoopReady()
{
    qDebug() << "VDRThread::eventLoopReady called";

    m_mutex.lock();
    m_eventLoopReady = true;
    m_mutex.unlock();
    m_waitingForEventLoop.wakeAll();
}


VDRFindServer* VDRThread::vdrFindServer()
{
    qDebug() << "VDRThread::vdrFindServer called";
    waitForEventLoop(); //Better save than sorry

    m_mutex.lock();

    emit vdrFindServerNeeded();
    m_waitingForClassInitialized.wait( &m_mutex );
    VDRFindServer* _vdrFindServer = m_vdrFindServer;

    m_mutex.unlock();
    return _vdrFindServer;
}


VDR* VDRThread::vdr()
{
    qDebug() << "VDRThread::vdr called";
    waitForEventLoop(); //Better save than sorry

    m_mutex.lock();

    if( !m_vdr )
    {
        emit vdrNeeded();
        m_waitingForClassInitialized.wait( &m_mutex );
    }
    else
    {
        qWarning() << "VDRThread::vdr called, but vdr already initialized";
    }
    VDR* _vdr = m_vdr;

    m_mutex.unlock();

    return _vdr;
}


void VDRThread::initVDRFindServer()
{
    qDebug() << "VDRThread::initVDRFindServer called";

    m_mutex.lock();

    m_vdrFindServer = new VDRFindServer( this );
    m_waitingForClassInitialized.wakeAll();

    m_mutex.unlock();
}


void VDRThread::initVDR()
{
    qDebug() << "VDRThread::initVDR called";
     //As we are using signal/slot, we are anyway in the eventLoop, so no deleteLater needed
    delete m_vdrFindServer; //not needed anymore
    m_vdrFindServer = 0L;

    m_mutex.lock(); //Lock is still needed for waitCondition

    m_vdr = new VDR( this );
    m_waitingForClassInitialized.wakeAll();

    m_mutex.unlock();
}
